package com.mine.service;

import com.mine.dao.OrderMapper;
import com.mine.pojo.Meal;
import com.mine.pojo.Order;
import com.mine.pojo.OrderItem;
import com.mine.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class OrderService {

    @Autowired
    OrderMapper orderMapper;


    //添加到购物车
    @Transactional
    public void addOrderItem(Meal meal, HttpSession session) {
        User user = (User) session.getAttribute("user");
        OrderItem orderItem = orderMapper.queryOrderItemByUserByMeal(meal.getMeal_id(), user.getUser_id());
        if (orderItem == null) {
            orderMapper.addOrderItem(meal.getMeal_id(), user.getUser_id());
        } else {
            orderMapper.updateOrderItem(meal.getMeal_id(), user.getUser_id());
        }
    }

    //查看购物车
    @Transactional
    public List<OrderItem> queryAllOrderItemByUser(User user) {
        List<OrderItem> orderItemList = orderMapper.queryAllOrderItemByUser(user.getUser_id());
        return orderItemList;
    }

    //购物车商品减少数量
    @Transactional
    public void reduceOrderItemNum(Integer meal_id, int user_id) {
        orderMapper.reduceOrderItemNum(meal_id, user_id);
    }

    //购物车商品增加数量
    @Transactional
    public void addOrderItemNum(Integer meal_id, int user_id) {
        orderMapper.updateOrderItem(meal_id, user_id);
    }

    //购物车商品删除
    @Transactional
    public void deletemealcart(Integer meal_id, int user_id) {
        orderMapper.deletemealcart(meal_id, user_id);
    }

    //添加订单
    @Transactional
    public void addOrder(User user, String money, Order order) {
        //新增订单
        orderMapper.addOrder(user.getUser_id(), money, order);
        //通过id查找最后一个订单id
        Order order1 = orderMapper.findOrderLastById(null);
        //购物车关联order订单
        orderMapper.updateOrderItemByOrder(order1.getOrder_id(), user.getUser_id());
        //修改购物车状态
        orderMapper.updateOrderItemByState(user.getUser_id());
        //return order1;
    }

    //付款
    @Transactional
    public void paySuccess(String order_id) {
        orderMapper.paySuccess(order_id);
        List<OrderItem> orderItems = orderMapper.queryAllOrderItemByOrder(order_id);
        for (OrderItem orderItem : orderItems) {
            orderMapper.updateMealSize(orderItem.getMeal().getMeal_id());
        }
    }

    @Transactional
    public List<Order> queryOrderByUserId(int user_id) {
        return orderMapper.queryOrderByUserId(user_id, "0");
    }

    @Transactional
    public List<Order> queryAllOrder(User user, String state) {
        List<Order> orderList = orderMapper.queryOrderByUserId(user.getUser_id(), state);
        return orderList;
    }

    //取消订单
    @Transactional
    public void overOrder(Integer order_id) {
        orderMapper.deleteOrderItem(order_id);
        orderMapper.overOrder(order_id);
    }

    //查看订单
    @Transactional
    public List<OrderItem> queryAllOrderItemByOrder(String order_id) {
        return orderMapper.queryAllOrderItemByOrder(order_id);
    }

    @Transactional
    public Order queryOrderById(String order_id) {
        return orderMapper.queryOrderById(order_id);
    }

    @Transactional
    public OrderItem getOrderitemById(String orderitem_id) {
        return orderMapper.getOrderitemById(orderitem_id);
    }

    //查看订单
    @Transactional
    public List<Order> adminOrderList(String order_state) {
        return orderMapper.adminOrderList(order_state);
    }

    public void updateOrdershopstate(String order_id) {
        orderMapper.updateOrdershopstate(order_id);
    }

    public void updateOrdershopstate1(String order_id) {
        orderMapper.updateOrdershopstate1(order_id);
    }
}
